<?php

use Nette\Application\UI;

class RegisterPresenter extends BasePresenter
{


	/**
	 * Sign in form component factory.
	 * @return Nette\Application\UI\Form
	 */
	protected function createComponentRegisterForm()
	{
		$form = new UI\Form;
		$form->addText('username', 'Username:')
			->setRequired('Please provide a username.');

		$form->addPassword('password', 'Password:')
			->setRequired('Please provide a password.');
		
		$gender = array(
		    '1' => 'Muž',
		    '0' => 'Žena'
		);
		$form->addSelect('gender', 'Pohlavie:', $gender)
		    ->setRequired('Prosim zvolte pohlavie !');

		$form->addSubmit('send', 'Zaregistrovat');
		
		$form->addProtection('Vyprsala platnost, zadajte este raz');

		$form->onSuccess[] = callback($this, 'registerFormSubmitted');
		return $form;
	}



	public function RegisterFormSubmitted($form)
	{
		try {
			$values = $form->getValues();
			$row = $this->model->getUsers()->where(array('username' => $values->username))->count();
			if($row > 0) {
			    throw new RuntimeException("Zadane meno uz existuje, zvolte ine.");
			}
			unset($row);
			$this->model->getUsers()->insert(array(
			    'username' => $form->values->username,
			    'password' => $form->values->password,
			    'gender' => $form->values->gender,
			    'premium' => '0',
			    'dateCreated' => new Nette\DateTime()
			));	
			
			$this->redirect('Homepage:');

		} catch (RuntimeException $e) {
			$form->addError($e->getMessage());
		}
	}

}
